import { Tray } from 'electron';
import path from 'path';

import eventEmitter from './eventEmitter';
import store from './store';
const isMac = process.platform === 'darwin';

const createRecordTray = () => {
  if (isMac) {
    // const tray = new Tray(path.join(__dirname, '../public/pauseTemplate.png'));
    const startTray = new Tray(path.join(__dirname, '../public/startTemplate.png'));
    const stopTray = new Tray(path.join(__dirname, '../public/stopTemplate.png'));
    store.setState({
      startTray,
      stopTray
    });
    // tray.setToolTip('暂停');
    startTray.on('click', () => {
      store.state.mainWindow?.webContents?.send(
        'operateRecorder',
        store.state.recordStatus === 'recording' ? 'pause' : 'continue'
      );
      eventEmitter.emit('startRecord');
    });
    stopTray.on('click', () => {
      store.state.mainWindow?.webContents?.send('operateRecorder', 'stop');
      store.state.mainWindow?.show();
      store.setState({
        recordStatus: 'start'
      });
    });
    // tray.setTitle(new Date());
    startTray.setToolTip('开始/继续');
    stopTray.setToolTip('开始/继续');

    eventEmitter.on('startRecord', () => {
      if (store.state.recordStatus === 'start' || store.state.recordStatus === 'pause') {
        store.state.startTray?.setImage(path.join(__dirname, '../public/pauseTemplate.png'));
        store.setState({
          recordStatus: 'recording'
        });
      } else {
        store.state.startTray?.setImage(path.join(__dirname, '../public/startTemplate.png'));
        store.setState({
          recordStatus: 'pause'
        });
      }
    });
    eventEmitter.on('stopRecord', () => {
      store.state.startTray?.setImage(path.join(__dirname, '../public/startTemplate.png'));
      store.setState({
        recordStatus: store.state.recordStatus === 'start'
      });
    });
  }
};

export { createRecordTray };
